package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/users")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public Result list(UserDto userDto) {
        log.info("用户列表查询: {}", userDto);
        PageResult<User> pageResult = userService.list(userDto);
        return Result.success(pageResult);
    }
    @Log
    @PostMapping
    public Result add(@RequestBody User user) {
        userService.addUser(user);
        return Result.success();
    }
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        userService.deleteBYId(ids);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result GetById(@PathVariable Integer id){
        User user = userService.getById(id);
        return Result.success(user);
    }
    @Log
    @PutMapping
    public Result upDate(@RequestBody User user) {
        userService.upDate(user);
        return Result.success();
    }
    @GetMapping("/role/{roleLabel}")
    public Result listByRole(@PathVariable String roleLabel) {
        List<User> list = userService.listByRole(roleLabel);
        return Result.success(list);
    }
}
